package com.webproject.action.sysManager;

import com.webproject.core.BaseAction;
import com.webproject.core.Page;
import com.webproject.entity.sysManager.Role;
import com.webproject.entity.sysManager.RoleMenuRelation;
import com.webproject.service.sysManager.RoleManagerService;

public class RoleManagerAction extends BaseAction {

	private RoleManagerService roleManagerService;
	private Page mpage;
	public Page getMpage() {
		return mpage;
	}

	public void setMpage(Page mpage) {
		this.mpage = mpage;
	}

	public void setRoleManagerService(RoleManagerService roleManagerService) {
		this.roleManagerService = roleManagerService;
	}
	
	public String queryRoles(){
		mpage=getPage();
		String userId=getRequest().getParameter("userId");
		mpage.setCondition(userId);
		roleManagerService.queryRoles(mpage);
		setTotal(mpage);
		return SUCCESS;
	}
	public String editRoles(){
		String oper=getOper();
		Role role=getRole();
		if(oper.equals("add")){
			roleManagerService.saveRole(role);
		}else if (oper.equals("edit")){
			roleManagerService.editRole(role);
		}else if (oper.equals("del")){
			roleManagerService.delRole(role);
		}
		return SUCCESS;
	}
	private Role getRole(){
		String id=getRequest().getParameter("id");
		String name=getRequest().getParameter("name");
		log.debug("name---------------------:"+name);
		String description=getRequest().getParameter("description");
		String hasUse=getRequest().getParameter("hasUse");
		Role role=new Role();
		if(isNotEmpty(id)){
			role.setId(Integer.valueOf(id));
		}
		role.setName(name);
		role.setDescription(description);
		role.setHasUse(Boolean.valueOf(hasUse));
		return role;
	}
	public String addRoleMenuRelation(){
		RoleMenuRelation relation=getRoleMenu();
		roleManagerService.saveRoleMenuRelation(relation);
		return SUCCESS;
	}
	public String delRoleMenuRelation(){
		RoleMenuRelation relation=getRoleMenu();
		roleManagerService.delRoleMenuRelation(relation);
		return SUCCESS;
	}
	private RoleMenuRelation getRoleMenu(){
		RoleMenuRelation relation=new RoleMenuRelation();
		relation.setRoleId(Integer.valueOf(getRequest().getParameter("roleId")));
		relation.setMenuId(Integer.valueOf(getRequest().getParameter("menuId")));
		return relation;
	}
	
}
